package cn.itcast.oa.view.action;

import java.util.List;

import javax.annotation.Resource;

import cn.itcast.oa.base.BaseAction;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.oa.domain.Role;
import cn.itcast.oa.service.RoleService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class RoleAction extends BaseAction<Role> {

    @Resource
    private RoleService roleService;

    private Long id;
    private String name;
    private String description;

    private Role model = new Role();

    public Role getModel() {
        return model;
    }

    public String test() throws Exception {
        return "test";
    }

    /**
     * 列表
     */
    public String list() throws Exception {
        List<Role> roleList = roleService.findAll();
        ActionContext.getContext().put("roleList", roleList);
        return "list";
    }

    /**
     * 删除
     */
    public String delete() throws Exception {
        roleService.delete(model.getId());
        return "toList";
    }

    /**
     * 添加页面
     */
    public String addUI() throws Exception {
        return "saveUI";
    }

    /**
     * 添加
     */
    public String add() throws Exception {
        // 封装到对象中
//        Role role = new Role();
//        role.setName(model.getName());
//        role.setDescription(model.getDescription());
//
//        // 保存到数据库
//        roleService.save(role);

		roleService.save(model);

        return "toList";
    }

    /**
     * 修改页面
     */
    public String editUI() throws Exception {
        // 准备回显的数据
        Role role = roleService.getById(model.getId());
//		ActionContext.getContext().getValueStack().push(role);
        this.name = role.getName();
        this.description = role.getDescription();

        return "saveUI";
    }

    /**
     * 修改
     */
    public String edit() throws Exception {
        // 1，从数据库中获取原对象
        Role role = roleService.getById(model.getId());

        // 2，设置要修改的属性
        role.setName(model.getName());
        role.setDescription(model.getDescription());

        // 3，更新到数据库
        roleService.update(role);

        return "toList";
    }

    // ---

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
